//
//  SISSKPIEnrollFinishResultViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/31.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSKPIEnrollFinishResultViewController.h"
#import "SISSKPIDetailViewController.h"
#import "SISSKPIEnrollViewController.h"
#import "SISSTabBarController.h"

@interface SISSKPIEnrollFinishResultViewController ()

@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UILabel *finishTipLabel;
@property (weak, nonatomic) UILabel *finishTipInfoLabel;
@property (weak, nonatomic) UIButton *checkDetailButton;
@property (weak, nonatomic) UIButton *continueEnrollButton;
@property (weak, nonatomic) UIButton *backHomeButton;

@end

@implementation SISSKPIEnrollFinishResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"业绩指标申报";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back {
    
    if (self.isChange) {
        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:NSClassFromString(@"SISSMyKPIViewController")]) {
                [self.navigationController popToViewController:controller animated:YES];
            }
        }
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

/// 查看详情
- (void)checkDetailButtonClickAction {
    
    SISSKPIDetailViewController *vc = [[SISSKPIDetailViewController alloc] init];
    vc.isAppyIn = NO;
    vc.ID = self.ID;
    vc.isChange = self.isChange;
    [self.navigationController pushViewController:vc animated:YES];
}

/// 继续申报
- (void)continueEnrollButtonClickAction {
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[SISSKPIEnrollViewController class]]) {
            SISSKPIEnrollViewController *vc = (SISSKPIEnrollViewController *)controller;
            vc.isContinueEnroll = YES;
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}

/// 返回首页
- (void)backHomeButtonClickAction {
    if (self.isChange) {
        SISSTabBarController *tabBarCtr = [[SISSTabBarController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController = tabBarCtr;
    } else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)setupSubviews {
    [super setupSubviews];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"KPI_success"];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    UILabel *finishTipLabel = [UILabel yyr_labelWithText:@"提交成功 待机构审核" fontSize:15 textColor:MAIN_TEXT_COLOR alignment:NSTextAlignmentCenter];
    [self.view addSubview:finishTipLabel];
    self.finishTipLabel = finishTipLabel;
    
    UILabel *finishTipInfoLabel = [UILabel yyr_labelWithText:@"您的业绩已经提交，您所属机构确认\n份额后记录评级指标；" fontSize:15 textColor:SECONDARY_TEXT_COLOR alignment:NSTextAlignmentCenter];
    [self.view addSubview:finishTipInfoLabel];
    self.finishTipInfoLabel = finishTipInfoLabel;
    
    UIButton *checkDetailButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkDetailButton setTitle:@"查看详情" forState:UIControlStateNormal];
    [checkDetailButton setBackgroundColor:MAIN_COLOR];
    [checkDetailButton addTarget:self action:@selector(checkDetailButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    checkDetailButton.layer.cornerRadius = 4;
    checkDetailButton.layer.masksToBounds = YES;
    [self.view addSubview:checkDetailButton];
    self.checkDetailButton = checkDetailButton;
    
    UIButton *continueEnrollButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [continueEnrollButton setTitle:@"继续申报" forState:UIControlStateNormal];
    [continueEnrollButton setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [continueEnrollButton setBackgroundColor:UIColor.whiteColor];
    [continueEnrollButton addTarget:self action:@selector(continueEnrollButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    continueEnrollButton.layer.cornerRadius = 4;
    continueEnrollButton.layer.masksToBounds = YES;
    continueEnrollButton.layer.borderColor = MAIN_COLOR.CGColor;
    continueEnrollButton.layer.borderWidth = 1;
    [self.view addSubview:continueEnrollButton];
    self.continueEnrollButton = continueEnrollButton;
    
    self.continueEnrollButton.hidden = self.isChange;
    
    UIButton *backHomeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backHomeButton setTitle:@"返回首页" forState:UIControlStateNormal];
    [continueEnrollButton setTitleColor:SECONDARY_TEXT_COLOR forState:UIControlStateNormal];
    [backHomeButton setBackgroundColor:UIColor.groupTableViewBackgroundColor];
    [backHomeButton addTarget:self action:@selector(backHomeButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    backHomeButton.layer.cornerRadius = 4;
    backHomeButton.layer.masksToBounds = YES;
    [self.view addSubview:backHomeButton];
    self.backHomeButton = backHomeButton;
}

- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGSize imageSize = CGSizeMake(88, 88);
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom).offset(3 * globelMargin);
        make.size.mas_equalTo(imageSize);
    }];
    
    [self.finishTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imageView.mas_bottom).offset(2 * globelMargin);
        make.centerX.equalTo(weakSelf.view);
    }];
    
    [self.finishTipInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.finishTipLabel.mas_bottom).offset(2 * globelMargin);
        make.centerX.equalTo(weakSelf.view);
    }];
    
    [self.checkDetailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.finishTipInfoLabel.mas_bottom).offset(4 * globelMargin);
        make.left.equalTo(weakSelf.view).offset(2 * globelMargin);
        make.right.equalTo(weakSelf.view).offset(-2 * globelMargin);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
    [self.continueEnrollButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.checkDetailButton.mas_bottom).offset(2 * globelMargin);
        make.left.right.height.equalTo(weakSelf.checkDetailButton);
    }];
    
    [self.backHomeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.isChange) {
            make.top.equalTo(weakSelf.checkDetailButton.mas_bottom).offset(2 * globelMargin);
        } else {
            make.top.equalTo(weakSelf.continueEnrollButton.mas_bottom).offset(2 * globelMargin);
        }
        make.left.right.height.equalTo(weakSelf.continueEnrollButton);
    }];
}

@end
